package com.chhh.cranelease.service.impl;

import com.chhh.cranelease.config.PrereadConfig;
import com.chhh.cranelease.entity.CarInfo;
import com.chhh.cranelease.entity.CraneBrand;
import com.chhh.cranelease.entity.Staff;
import com.chhh.cranelease.mapper.BaseInfoMapper;
import com.chhh.cranelease.mapper.IntegratedmanageMapper;
import com.chhh.cranelease.model.CarInfoDriver;
import com.chhh.cranelease.model.DriverInfoModel;
import com.chhh.cranelease.service.BaseInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.List;

@Service
public class BaseInfoServiceImpl implements BaseInfoService {

    @Autowired
    private BaseInfoMapper baseInfoMapper;

    @Autowired
    private IntegratedmanageMapper integratedmanageMapper;

    @Autowired
    private PrereadConfig config;

    @Override
    public List<CarInfoDriver> selectAll(String c_num, String c_people) {
        return baseInfoMapper.selectAllCarInfo(c_num,c_people);
    }

    // 插入吊车
    @Override
    public void insertNewCar(CarInfo carInfo) {
        baseInfoMapper.insertNewCar(carInfo);
    }
    // 修改
    @Override
    public void updateThisCar(CarInfo carInfo) {
       baseInfoMapper.updateThisCar(carInfo);
    }

    @Override
    public List<DriverInfoModel> selectAllDriver(String s_card, String s_name, String s_sex, String sName, String s_intime, String s_outtime) {
        return baseInfoMapper.selectAllDriver(s_card,s_name,s_sex,s_intime,s_outtime);
    }

    @Override
    public Integer updateThisDriver(Staff staff) {
        if (staff.getS_state()==0){
            integratedmanageMapper.updatePeopleLeave(staff);
        }
        return baseInfoMapper.updateThisDriver(staff);
    }

    @Override
    public Integer insertNewDriver(Staff staff) {
        if (staff.getS_state()==1){
            integratedmanageMapper.insertPeopleLog(staff);
        }
        return baseInfoMapper.insertNewDriver(staff);
    }

    @Override
    public void deleteThisCar(Integer[] c_id) {
         baseInfoMapper.deleteThoseCar(c_id);
    }


    // 车型信息




    @Override
    public Integer insertStyle(MultipartFile images, Integer model_id, String description)  {

        File path = new File(config.getUploadpath()+File.separator+model_id);
        if (!path.exists()){
            path.mkdirs();
        }

        String newFileName = System.currentTimeMillis()+"_"+images.getOriginalFilename();
        String surfaceName = "upload"+File.separator+model_id+File.separator+newFileName;
        File newFile = new File(path,newFileName);
        try {
            FileOutputStream fos = new FileOutputStream(newFile);
            InputStream is = images.getInputStream();
            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = is.read(buffer)) != -1) {
                fos.write(buffer, 0, len);
            }
            fos.close();
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Integer count = baseInfoMapper.insertStyle(surfaceName,model_id,description);
        return count;
    }

    @Override
    public String selectStyle(String model_id) {
        return baseInfoMapper.selectStyle(model_id);
    }

    @Override
    public List<CraneBrand> getImage() {
        return baseInfoMapper.getImage();
    }

    @Override
    public CraneBrand getImages(Integer model_id) {

        CraneBrand craneBrand = baseInfoMapper.getImages(model_id);
        /*String path = config.getUploadpath()+craneBrand.getImages();*//*
        File paths = new File(config.getUploadpath()+File.separator+model_id);
        File[] files = paths.listFiles();*/
        /*String path = craneBrand.getImages(); *//*File.separator+model_id+File.separator+files[0].getName();*//*
        System.out.println(path);
        craneBrand.setImages(path);*/
        return craneBrand;
    }
}
